﻿using System;

namespace Common
{
    //系统唯一值生成器
    public class IdGenerator
    {
        private const long X = 387420489;
        private const long Y = 513180409;

        public static long Obfuscate(long id)
        {
            return id * X % 1000000000;
        }

        /// <summary>
        /// 根据数据库主键生成唯一数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string ObfuscateHex(long id)
        {
            return Obfuscate(id).ToString("X8");
        }

        public static long Deobfuscate(long obfuscateId)
        {
            return obfuscateId * Y % 1000000000;
        }

        public static long DeobfuscateHex(string obfuscateIdHex)
        {
            long obfuscateId = Convert.ToInt64(obfuscateIdHex, 16);
            return Deobfuscate(obfuscateId);
        }
    }
}